package org.springleaf.utils;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springleaf.core.mvc.interceptor.ContextHolder;

/**
 * 提供对request,respones,session等对象的操作
 * @author leaf
 */
@SuppressWarnings("unchecked")
public class WebHelper{

	public static Log log = LogFactory.getLog("springleaf");

	/**
	 * 得到基本的Http资源路径
	 * 
	 * @param request
	 * @return
	 */
	public static String getBaseHref() {
		HttpServletRequest request = getReq();
		StringBuilder builder = new StringBuilder();
		builder.append(request.getScheme());
		builder.append("://");
		builder.append(request.getServerName());
		builder.append(':');
		builder.append(request.getServerPort());
		builder.append(request.getContextPath());
		return builder.toString();
	}
	

	/**
	 * 将response响应头设置为图片
	 */
	public static HttpServletResponse setHttpResponseImageHeader() {
		HttpServletResponse response = getResp();
		response.setContentType("image/jpeg");// 设定response的内容类型为图片
		response.setHeader("Cache-Control", "no-store");
		response.setHeader("Pragrma", "no-cache");// 设定response的缓存机制
		response.setDateHeader("Expires", 0);
		return response;
	}

	/**
	 * 得到Request
	 */
	public static HttpServletRequest getReq() {
//		return ((ServletRequestAttributes) RequestContextHolder
//				.getRequestAttributes()).getRequest();
		return ContextHolder.getRequest();
	}
	
	/**
	 * 得到Response
	 */
	public static HttpServletResponse getResp(){
		return ContextHolder.getResponse();
	}
	
	/**
	 * 得到ServletContext
	 */
	public static ServletContext getServletContext(){
		return getReq().getServletContext();
	}

	/**
	 * 得到session
	 */
	public static HttpSession getSes() {
		return getReq().getSession();
	}

	/**
	 * 得到Session中name对应的Attribute
	 */
	public static <T> T getSesAttr(String name) {
		return (T) getSes().getAttribute(name);
	}

	/**
	 * 往Session的Attribute中放值
	 */
	public static HttpSession setSesAttr(String name, Object v) {
		HttpSession session = getSes();
		session.setAttribute(name, v);
		return session;
	}

	/**
	 * 删除Session中的Attribute
	 */
	public static HttpSession removeSesAttr(String name) {
		HttpSession session = getSes();
		session.removeAttribute(name);
		return session;
	}

	/**
	 * 得到Request中name对应的Attribute
	 */
	public static <T> T getAttr(String name) {
		return (T) getReq().getAttribute(name);
	}

	/**
	 * 往Request的Attribute中放值
	 */
	public static HttpServletRequest setAttr(String name, Object v) {
		HttpServletRequest request = getReq();
		getReq().setAttribute(name, v);
		return request;
	}

	/**
	 * 删除Request的Attribute中指定的数据
	 */
	public static HttpServletRequest removeAttr(String name) {
		HttpServletRequest request = getReq();
		getReq().removeAttribute(name);
		return request;
	}

	/**
	 * 得到Request paramster 中 name 的值
	 */
	public static String getParam(String name) {
		return getReq().getParameter(name);
	}

	/**
	 * 得到Request paramster 中 name 的值
	 */
	public static String[] getParams(String name) {
		return getReq().getParameterValues(name);
	}
}
